package exam.exam05;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    //设计一个多线程的程序如下：设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出，现在有5个售票点
    //同时售票，用5个线程模拟这5个售票点的售票情况。

    //共享数据为：100张火车票  和  任务：卖票
    public static void main(String[] args) {
        Ticket t = new Ticket();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);
        Thread t4 = new Thread(t);
        Thread t5 = new Thread(t);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t4.setName("窗口4");
        t5.setName("窗口5");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }
}

class Ticket implements Runnable {
    private int ticket = 100;//票数  共享数据
    Lock lock = new ReentrantLock(); //创建锁对象

    @Override
    public void run() {
        lock.lock();
        while (ticket > 0) {//任务为共享数据
            System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
            ticket--;
        }
        lock.unlock();
    }

}